package com.ceres.device.controller;


import com.ceres.device.entity.QueryPageBean;
import com.ceres.device.pojo.Device;
import com.ceres.device.service.DeviceService;
import com.ceres.entity.PageResult;
import com.ceres.entity.Result;
import com.ceres.entity.StatusCode;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 *  设备信息的服务类
 */
@RestController
@RequestMapping("/facility")
public class DeviceController {
    @Autowired
    DeviceService deviceService;

    /**
     * 根据智能设备实体进行添加
     *
     * @param device
     * @return
     */
    @PostMapping("/add")
    public Result deviceAdd(@RequestBody Device device) {
        try {

            deviceService.deviceAdd(device);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
        return new Result(true, StatusCode.OK);
    }

    /**
     * 根据智能设备实体进行修改
     *
     * @param device
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Device device) {
        try {
            deviceService.deviceUpdate(device);
            return new Result(true, StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }

    }


    /**
     * 设备信息的逻辑删除
     *
     * @param device
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Device device) {
        try {
            deviceService.deviceDelete(device);
            return new Result(true, StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }

    }

    /**
     * 查询数据库中的所有智能设备以及设备信息
     *
     * @return
     */
    @PostMapping("/findAll")
    public Result findAll() {
        try {
            List<Device> mapList = deviceService.findAll3();
            return new Result(true, StatusCode.OK, mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
    }

    /**
     * 根据用户的ID 查询单个智能设备的对象
     *
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        try {
            Device device = deviceService.findById(id);
            return new Result(true, StatusCode.OK,device);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
    }

    /**
     * 查询数据库中的所有智能设备以及设备信息
     *
     * @return
     */
/*    @PostMapping("/findPage/{page}/{size}")
    public Result findPage(@PathVariable("page") int page,@PathVariable("size")int size) {
        try {
                Page<Device> servicePageInfo = deviceService.findPage(page,size);
                //总页数
                long total = servicePageInfo.getTotal();
                //返回的参数
                List<Device> result = servicePageInfo.getResult();
                PageResult pageResult = new PageResult(total, result);
                return new Result(true, StatusCode.OK, pageResult);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
    }*/

    /**
     * 查询数据库中的所有智能设备以及设备信息
     *
     * @return
     */
    @PostMapping("/findPageQuery")
    public Result findPageQuery(@RequestBody QueryPageBean queryPageBean) {
        try {
            Page<Device> servicePageInfo = deviceService.findPageQuery(queryPageBean);
            //总页数
            long total = servicePageInfo.getTotal();
            //返回的参数
            List<Device> result = servicePageInfo.getResult();
            PageResult pageResult = new PageResult(total,result);
            return new Result(true, StatusCode.OK, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
    }
}


